var querystring = require("querystring");
var formidable = require("formidable");

function start(response) {

    console.log("Request handler 'start' was called.");

    var body = '<html>' +
        '<head>' +
        '<meta http-equiv="Content-Type" content="text/html; ' +
        'charset=UTF-8" />' +
        '</head>' +
        '<body>' +
        '<form name ="input" action = "./dbtest" method = "post">' +
        'Username : <input type="text" name = "username"><br>' +
        'Password : <input type ="password" name = "password">' +
        '<input type="submit" value="Submit" />' +
        '</form>' +
        '</body>' +
        '</html>';


    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(body);
    response.end();
}

function login(response, post) {

    console.log("Request handler 'login' was called");
    response.writeHead(200, {"Content-Type": "text/plain"});
    //response.write("you've sent: " + querystring.parse(postData).text);
    post('/login', function (req, res) {
        new user({
            username: req.body.username,
            password: req.body.password
        }).save(function (err, doc) {
                if (err) res.json(err);
                else    res.send('Succesfully inserted');
            });
    });
    response.end();

}

exports.start = start;
exports.login = login;